﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using AForge.Imaging;
using Xceed.Wpf.Toolkit;
using GamePlugin.Logics.Core;
using System.Drawing.Imaging;
using GamePlugin.IO;
using WindowsInput;
using GamePlugin.Logics;

namespace GamePlugin.GGraphics
{
    public class ScreenChecker
    {
        public Bitmap ScreenShot
        {
            get;
            set;
        }

        public Bitmap CursorIcon
        {
            get;
            set;
        }

        private Font _Font = new Font(FontFamily.GenericSerif, 10);
        private Brush _Brush = new SolidBrush(Color.Wheat);
        public void DrawString(string text)
        {
            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                g.DrawString(text, _Font, _Brush, 100, 100);
            }
        }

        public void GetScreenData()
        {
            //this.CursorIcon = CaptureScreen.CaptureCursor();
            this.ScreenShot = CaptureScreen.CaptureDesktopScreen();
            this.ScreenShot = this.ScreenShot.Clone(new Rectangle(0, 0, this.ScreenShot.Width, this.ScreenShot.Height), PixelFormat.Format24bppRgb);
        }

        public Rectangle FindImage(Bitmap sourceImage, Bitmap template, float similar = 0.95f)
        {
            Rectangle ret = new Rectangle();

            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(similar);
            TemplateMatch[] matchings = tm.ProcessImage(sourceImage, template);

            if (matchings.Length >= 1)
            {
                int selected = 0;
                for (int i = 1; i < matchings.Length; i++)
                    if (matchings[i].Rectangle.Left < matchings[selected].Rectangle.Left)
                    {
                        selected = i;
                        break;
                    }
                    ret = matchings[0].Rectangle;
            }

            return ret;
        }

        public bool CheckMouseOverImage(Bitmap template, float similar = 0.9f)
        {
            Rectangle ret = new Rectangle();
            Rectangle sourceRect = new Rectangle();

            int tempX = (template.Width + 3);
            int tempY = (template.Height + 3);
            try
            {
                Point pos = Helper.CursorPosition;
                sourceRect = new Rectangle(pos.X - tempX, pos.Y - tempY, tempX * 2, tempY * 2);
                Bitmap sourceImage = this.ScreenShot.Clone(sourceRect, PixelFormat.Format24bppRgb);

                ret = FindImage(sourceImage, template, similar);
            }
            catch (Exception e)
            {
                //Helper.Log(e.ToString());
                return false;
            }

            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;
            if (ret.Width > 0 && ret.Height > 0)
            {
                Helper.MoveCursor(sourceRect.X + ret.X + ret.Width / 2, sourceRect.Y + ret.Y + ret.Height / 2);
                Helper.Sleep(100);
            }

            return ret.Width > 0 && ret.Height > 0;
        }

        public string GetTextMouseOver(Bitmap bitmap)
        {
            int tempX = (bitmap.Width / 3 * 2);
            int tempY = (bitmap.Height / 3 * 2);

            Point pos = Helper.CursorPosition;
            Rectangle sourceRect = new Rectangle(pos.X - tempX, pos.Y - tempY, tempX * 2, tempY * 2);
            Bitmap sourceImage = this.ScreenShot.Clone(sourceRect, PixelFormat.Format24bppRgb);

            return GetTextFromImage(sourceImage);
        }

        public string GetTextFromImage(Bitmap bitmap)
        {
            string ret = null;

            string fileName = IOManager.Instance.SaveImageToFile(bitmap);
            MODI.Document md = new MODI.Document();
            md.Create(fileName);
            md.OCR(MODI.MiLANGUAGES.miLANG_CHINESE_TRADITIONAL, true, true);
            MODI.Image image = (MODI.Image)md.Images[0];
            ret = image.Layout.Text;

            return ret;
        }
    }
}
